﻿using PwjCy.Lite.EntityLite;
using PwjCy.Lite.EntityLite.Imp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace PwjCy.Lite.Persistence.Imp
{
    /// <summary>
    /// SqlBuilder委托处理数据库字段输出格式
    /// </summary>
    /// <param name="outStr"></param>
    /// <param name="pro"></param>
    public delegate void SqlBuilderDelegateHandlerFieldFormat(ref string outStr, PropertyInfo pro);
    public enum DelegateFieldTypeEnum
    {
        /// <summary>
        /// 插入字段
        /// </summary>
        Insert = 1,
        /// <summary>
        /// 插入字段值参数化
        /// </summary>
        InsertParm = 2,
        /// <summary>
        /// 更新字段设置值
        /// </summary>
        UpdateParm = 3,
        /// <summary>
        /// 查询字段
        /// </summary>
        Select = 4,
    }

    public abstract class SqlBuilderHandler : ISqlBuilder
    {
        private IDictionary<DelegateFieldTypeEnum, SqlBuilderDelegateHandlerFieldFormat> DictionaryDelegateFieldType
        {
            get
            {
                return new Dictionary<DelegateFieldTypeEnum, SqlBuilderDelegateHandlerFieldFormat>(){
                {DelegateFieldTypeEnum.Insert,DataFieldJoinWithComma},
                {DelegateFieldTypeEnum.InsertParm,DataFieldParmJoinWithComma},
                {DelegateFieldTypeEnum.UpdateParm,UpdateDataFieldParmJoinWithComma},
                {DelegateFieldTypeEnum.Select,DataFieldJoinWithComma},
                };
            }
        }
        public string SaveSql(Type pwjcyObjectType, string tableName, ObjectOperateType objectOperateType)
        {
            switch (objectOperateType)
            {
                case EntityLite.ObjectOperateType.Create:
                    return InsertSql(pwjcyObjectType, tableName);
                case EntityLite.ObjectOperateType.Delete:
                    return DeleteSql(pwjcyObjectType, tableName);
                case EntityLite.ObjectOperateType.Update:
                    return UpdateSql(pwjcyObjectType, tableName);
                default:
                    throw new Exception("未知的操作类型" + pwjcyObjectType);
            }
        }
        public abstract string LoadPwjCyObjectSql(Type pwjcyObjectType, string tableName);
        protected string DataFieldOutputSqlHandler(Type pwjcyObjectType, DelegateFieldTypeEnum handlerType)
        {
            string insertFields = string.Empty;
            foreach (PropertyInfo pro in EntityLiteRelationMap.Instance.PwjCyOjbectTypeDescription(pwjcyObjectType).PwjCyFields)
            {
                DictionaryDelegateFieldType[handlerType](ref insertFields, pro);
            }
            return insertFields.Substring(1);
        }
        protected abstract string InsertSql(Type pwjcyObjectType, string tableName);
        protected abstract string DeleteSql(Type pwjcyObjectType, string tableName);
        protected abstract string UpdateSql(Type pwjcyObjectType, string tableName);
        /// <summary>
        /// [A],[B],[C]格式
        /// </summary>
        /// <param name="outStr"></param>
        /// <param name="pro"></param>
        protected abstract void DataFieldJoinWithComma(ref string outStr, PropertyInfo pro);
        /// <summary>
        /// @A,@B,@C格式
        /// </summary>
        /// <param name="outStr"></param>
        /// <param name="pro"></param>
        protected abstract void DataFieldParmJoinWithComma(ref string outStr, PropertyInfo pro);
        /// <summary>
        /// [A]=@A,[B]=@B,[C]=@C
        /// </summary>
        /// <param name="outStr"></param>
        /// <param name="pro"></param>
        protected abstract void UpdateDataFieldParmJoinWithComma(ref string outStr, PropertyInfo pro);
    }
}
